package com.yuer.tools.myfileziptools.utils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.tang
 * @version 1.0
 * @date 2021/1/28 10:07
 */
public class JsonUtil {

    /**
     * 将返回的json数据转为map
     * @param str
     * @return
     */
    public static Map<String, Object> json2map(String str) {
        Map<String, Object> res = null;
        try {
            Gson gson = new Gson();
            res = gson.fromJson(str, new TypeToken<Map<String, Object>>() {}.getType());
        } catch (JsonSyntaxException e) {
        }
        return res;
    }



    /**
     * 解决返回的list转换异常问题
     * @param jsonString
     * @param type
     * @return
     */
    public static <T> List<T> StringToTypelist(String jsonString, Class<T[]> type) {
        Gson gson = new Gson();
        T[] list  = gson.fromJson(jsonString, type);
        return Arrays.asList(list);
    }

    public static <T> List<T> StringTolist(String jsonString, Class<T> t) {
        Gson gson = new Gson();
        Type type=new TypeToken<List<T>>(){}.getType();
        T[] list  = gson.fromJson(jsonString, type);
        return Arrays.asList(list);
    }
}
